var gridStore;
 var ids="";
 var grid;
 var acbStore; 
 var distributor_id=""; 
 var ocbStore;
 var modelStore;
 var shuntStore;
Ext.define('app.controller.StockController', {
			extend : 'Ext.app.Controller',
			refs : [{
						ref : 'ViewportStock',
						selector : 'ViewportStock' 
					}],
			stores : ["Stocks",'ResourceComboBox','AccountComboBox','OperatorComboBox','ShuntComboBox','ModelComboBox'],
			init : function() {
				this.control({
							'button[name=addSubmit]' : {
								click : function(o) {
									this.onAddOrEditStock(o);
								}
							},
							'button[name=add]' : {
								click : function(o) { 
									
									var openWindow = Ext.widget('addStockView');
									openWindow.animateTarget = document.body;
									openWindow.title = "盒子入库";
									openWindow.show();
								}
							},
							'button[name=edit]' : {
								click : function(o) {
									//console.log(ocbStore);
									
									var rows = o.ownerCt.ownerCt.getSelectionModel().getSelection();
									if( rows.length>0){ 
										 
										var openWindow = Ext.widget('addStockView');
										openWindow.animateTarget = document.body;
										openWindow.items.items[0].items.items[0].setValue(rows[0].data["id"]);
										openWindow.items.items[0].items.items[1].setValue(rows[0].data["box_number"]);
										openWindow.items.items[0].items.items[1].setDisabled(true);
										openWindow.items.items[0].items.items[2].setDisabled(true);
										openWindow.items.items[0].items.items[2].setValue(rows[0].data["box_mac"]); 
										if(rows[0].data["operator"]!=null){
											openWindow.items.items[0].items.items[4].setValue(rows[0].data["operator"]["id"]);
										} 
										if(rows[0].data["shuntType"]!=null){
											openWindow.items.items[0].items.items[5].setValue(rows[0].data["shuntType"]["id"]);
										} 
										if(rows[0].data["modelType"]!=null){
											openWindow.items.items[0].items.items[6].setValue(rows[0].data["modelType"]["id"]);
										} 
										openWindow.items.items[0].items.items[7].setValue(rows[0].data["box_type"]);
										openWindow.items.items[0].items.items[8].setValue(rows[0].data["mem"]);
										openWindow.title = "盒子更新";
										openWindow.show();
									}else{
										Ext.Msg.alert('提示', "请选择需要更新的盒子!");
									}
									
								}
							},
							'button[name=stock_chu]' : {
								click : function(o) {
									distributor_id=""
									var rows = o.ownerCt.ownerCt.getSelectionModel().getSelection(); 
									if (rows.length > 0) {
										ids=rows[0].data["id"];
										var openWindow = Ext.widget('stockChuWindowView');
										openWindow.animateTarget = document.body;
										openWindow.title = "出库操作";
										openWindow.show();
									}else{
										Ext.Msg.alert('提示', "请选择出库盒子!");									
									}
								}
							},
							'button[name=allot_update]' : {
								click : function(o) {
									ids="";
									var rows = o.ownerCt.ownerCt.getSelectionModel().getSelection();// 父组件
									if (rows.length > 0) {
										var uIds = "";
										for (i = 0; i < rows.length; i++) {
											if(rows[i].data["resource"]==null){
												uIds="";
												Ext.Msg.alert('提示', rows[i].data["box_mac"]+"未分配资源!");
												break;
											}
											if (i < rows.length - 1) {
												uIds += rows[i].data["id"] + ",";
											} else {
												uIds += rows[i].data["id"];
											}
										}
										ids=uIds;
									} 
									 
									if(ids!=""){ 
										var openWindow = Ext.widget('msgWindowView');
										openWindow.animateTarget = document.body;
										openWindow.title = "提示";
										openWindow.items.items[0].items.items[0].setValue("是否给选中的盒子重置资源?"); 
										openWindow.items.items[0].items.items[2].setHidden(false);
										openWindow.items.items[0].items.items[4].setValue(3);
										openWindow.show();
									}
								}
							},
							/*'button[name=allot_update]' : {
								click : function(o) {
									ids="";
									var rows = o.ownerCt.ownerCt.getSelectionModel().getSelection();// 父组件
									if (rows.length > 0) {
										var uIds = "";
										for (i = 0; i < rows.length; i++) {
											if(rows[i].data["resource"]==null){
												uIds="";
												Ext.Msg.alert('提示', rows[i].data["box_mac"]+"未分配资源!");
												break;
											}
											if (i < rows.length - 1) {
												uIds += rows[i].data["id"] + ",";
											} else {
												uIds += rows[i].data["id"];
											}
										}
										ids=uIds;
									} 
									 
									if(ids!=""){ 
										Ext.Ajax.request({
										url : "allotResource.action",
										params : {
											"ids" : ids 
										},
										success : function(respose, option) {
											var isSuc = Ext.decode(respose.responseText);
											Ext.Msg.alert('提示', isSuc.msg);
											gridStore.load();
											o.ownerCt.ownerCt.close();
										},
										failure : function(form, action) {
														Ext.Msg.alert("提示", '出错！');
										}
									});
									}
								}
							},*/
							'button[name=allot_del]' : {
								click : function(o) {
									ids="";
									var rows = o.ownerCt.ownerCt.getSelectionModel().getSelection();// 父组件
									if (rows.length > 0) {
										var uIds = "";
										for (i = 0; i < rows.length; i++) {
											if(rows[i].data["resource"]==null){
												uIds="";
												Ext.Msg.alert('提示', rows[i].data["box_mac"]+"未分配资源!");
												break;
											}
											if (i < rows.length - 1) {
												uIds += rows[i].data["id"] + ",";
											} else {
												uIds += rows[i].data["id"];
												break;
											}
										}
										ids=uIds;
									}  
									if(ids!=""){ 
										 Ext.Ajax.request({
												url : "saveOrupdateStock.action",
												params : {
													"id" :ids ,
													"type":1
												},
												success : function(respose, option) {
													var result = Ext.decode(respose.responseText);
													Ext.Msg.alert('提示', result.msg);
													gridStore.load();
													 
												},
												failure : function(form, action) {
													Ext.Msg.alert("提示", '出错！');
												}
											});
									}
								}
							},
							'button[name=server_yes]' : {
								click : function(o) {
									ids=""; 
									var submitForm = o.ownerCt.ownerCt.items.items[0];
									if (!submitForm.getForm().isValid())
										return;
									var server_id=o.ownerCt.ownerCt.items.items[0].items.items[1].getValue();
									var rows = grid.getSelectionModel().getSelection();// 父组件
									if (rows.length > 0) {
										var uIds = "";
										for (i = 0; i < rows.length; i++) {
											if (i < rows.length - 1) {
												uIds += rows[i].data["id"] + ",";
											} else {
												uIds += rows[i].data["id"];
											}
										}
										ids=uIds;
										if(ids!=""){  
											Ext.Ajax.request({
												url : "updateServers.action",
												params : {
													"ids" :ids,
													"server_id":server_id
												},
												success : function(respose, option) {
													var result = Ext.decode(respose.responseText);
													Ext.Msg.alert('提示', result.msg);
													gridStore.load();
													o.ownerCt.ownerCt.close();
												},
												failure : function(form, action) {
													Ext.Msg.alert("提示", '出错！');
												}
											});
										} 
									} 
								}
							},
							'button[name=levelUP]' : {
								click : function(o) {
									ids=""; 
									var submitForm = o.ownerCt.ownerCt.items.items[0];
									if (!submitForm.getForm().isValid())
										return;
									var updateVersion=o.ownerCt.ownerCt.items.items[0].items.items[1].getValue();
									var rows = grid.getSelectionModel().getSelection();// 父组件
									if (rows.length > 0) {
										var uIds = "";
										for (i = 0; i < rows.length; i++) {
											if(updateVersion==rows[i].data["box_version"]){
												uIds="";
												Ext.Msg.alert('提示', rows[i].data["box_mac"]+"版本与指定版本一致!");
												break;
											}
											if (i < rows.length - 1) {
												uIds += rows[i].data["id"] + ",";
											} else {
												uIds += rows[i].data["id"];
											}
										}
										ids=uIds;
										if(ids!=""){  
											Ext.Ajax.request({
												url : "updateVersions.action",
												params : {
													"ids" :ids,
													"box_version":updateVersion
												},
												success : function(respose, option) {
													var result = Ext.decode(respose.responseText);
													Ext.Msg.alert('提示', result.msg);
													gridStore.load();
													o.ownerCt.ownerCt.close();
												},
												failure : function(form, action) {
													Ext.Msg.alert("提示", '出错！');
												}
											});
										} 
									} 
								}
							},
							'button[name=level_up]' : {
								click : function(o) {
									if( grid.getSelectionModel().getSelection().length>0){ 
										var openWindow = Ext.widget('levelUpWindowView');
										openWindow.animateTarget = document.body;
										openWindow.title = "盒子升级指定版本";
										openWindow.items.items[0].items.items[0].setValue("是否给选中盒子升级指定版本?");
										openWindow.show();
									}else{
										Ext.Msg.alert('提示', "请选择需要升级版本的盒子!");
									}
								}
							},
							'button[name=serverUP]' : {
								click : function(o) {
									if( grid.getSelectionModel().getSelection().length>0){ 
										var openWindow = Ext.widget('serverWindowView');
										openWindow.animateTarget = document.body;
										openWindow.title = "盒子指定服务器";
										openWindow.show();
									}else{
										Ext.Msg.alert('提示', "请选择需要指定服务器的盒子!");
									}
								}
							},
							'button[name=allot]' : {
								click : function(o) {
									ids="";
									var rows = o.ownerCt.ownerCt.getSelectionModel().getSelection();// 父组件
									if (rows.length > 0) {
										var uIds = "";
										for (i = 0; i < rows.length; i++) {
											if(rows[i].data["resource"]!=null){
												uIds="";
												Ext.Msg.alert('提示', rows[i].data["box_mac"]+"已分配资源!");
												break;
											}
											if (i < rows.length - 1) {
												uIds += rows[i].data["id"] + ",";
											} else {
												uIds += rows[i].data["id"];
											}
										}
										ids=uIds;
									} 
									 
									if(ids!=""){ 
										var openWindow = Ext.widget('msgWindowView');
										openWindow.animateTarget = document.body;
										openWindow.title = "提示";
										openWindow.items.items[0].items.items[0].setValue("是否给选中的盒子分配资源?");
										openWindow.items.items[0].items.items[2].setValue(2);
										openWindow.items.items[0].items.items[5].setHidden(false);
										openWindow.show();
									}
								}
							},
							'button[name=allots]' : {
								click : function(o) {
									var openWindow = Ext.widget('msgsWindowView');
									openWindow.animateTarget = document.body;
									openWindow.title = "提示";
									openWindow.items.items[0].items.items[0].setValue("是否把已分配的原IP资源替换空闲的目标IP资源?");
									openWindow.show();
								}
							},
							'button[name=del]' : {
								click : function(o) {
									var rows = o.ownerCt.ownerCt.getSelectionModel().getSelection();// 父组件
									var delIds = "";
									if (rows.length > 0) {
										for (i = 0; i < rows.length; i++) {
											if(rows[i].data["resource"]!=null){
												Ext.Msg.alert('提示', rows[i].data["box_mac"]+"会释放分配资源!"+rows[i].data["resource"].ip);
											}
											if (i < rows.length - 1) {
												delIds += rows[i].data["id"] + ",";
											} else {
												delIds += rows[i].data["id"];
											}
										}
									} 
									if(delIds!=""){ 
										Ext.Msg.confirm("系统信息", "删除后数据将不可恢复,确定要删除吗", function(btn) {
											if(btn=="yes"){
												Ext.Ajax.request({
													url : "deleteStocks.action",
													params : {
														"ids" :delIds
													},
													success : function(respose, option) {
														var result = Ext.decode(respose.responseText);
														Ext.Msg.alert('提示', result.msg);
														gridStore.load();
													},
													failure : function(form, action) {
														Ext.Msg.alert("提示", '出错！');
													}
												});
											}
										});
									}else{
										Ext.Msg.alert('提示', "请选择需要删除的盒子");
									}
								}
							},
							'button[name=msg_yes]' : {
								click : function(o) {
									var submitForm = o.ownerCt.ownerCt.items.items[0];
									if (!submitForm.getForm().isValid())
										return;
									Ext.Ajax.request({
										url : "allotResource.action",
										params : {
											"ids" : ids,
											"id":submitForm.items.items[1].getValue(),
											"del" :submitForm.items.items[2].getValue(),
											"box_type" :submitForm.items.items[3].items.items[0].checked?1:0,
											"work_type" : submitForm.items.items[4].getValue(),
											"times" : submitForm.items.items[5].getValue()
										},
										success : function(respose, option) {
											var isSuc = Ext.decode(respose.responseText);
											Ext.Msg.alert('提示', isSuc.msg);
											gridStore.load();
											o.ownerCt.ownerCt.close();
										},
										failure : function(form, action) {
														Ext.Msg.alert("提示", '出错！');
										}
									});
								}
							},
							'button[name=stockChu]' : {
								click : function(o) {
									var submitForm = o.ownerCt.ownerCt.items.items[0];
									if (!submitForm.getForm().isValid())
										return;
									Ext.Ajax.request({
										url : "stockChu.action",
										params : {
											"id" : ids,
											"distributor_id":submitForm.items.items[1].getValue() ,
											"account_id":submitForm.items.items[2].getValue() 
										},
										success : function(respose, option) {
											var isSuc = Ext.decode(respose.responseText);
											Ext.Msg.alert('提示', isSuc.msg);
											gridStore.load();
											o.ownerCt.ownerCt.close();
										},
										failure : function(form, action) {
														Ext.Msg.alert("提示", '出错！');
										}
									});
								}
							},
							'button[name=msgs_yes]' : {
								click : function(o) {
									var submitForm = o.ownerCt.ownerCt.items.items[0];
									if (!submitForm.getForm().isValid())
										return;
									Ext.Ajax.request({
										url : "allotResources.action",
										params : {
											"old_ip":submitForm.items.items[1].getValue(),
											"new_ip" :submitForm.items.items[2].getValue(),
											"del" :submitForm.items.items[3].getValue(),
											"box_type" :submitForm.items.items[4].items.items[0].checked?1:0,
											"work_type" :3
										},
										success : function(respose, option) {
											var isSuc = Ext.decode(respose.responseText);
											Ext.Msg.alert('提示', isSuc.msg);
											gridStore.load();
											o.ownerCt.ownerCt.close();
										},
										failure : function(form, action) {
														Ext.Msg.alert("提示", '出错！');
										}
									});
								}
							},
							'button[name=msg_no]' : {
								click : function(o) {
									 o.ownerCt.ownerCt.close();
								}
							}  
						});
			},
			onAddOrEditStock : function(o) {
				ids="";
				var submitFormAdd = o.ownerCt.ownerCt.items.items[0];
				if (!submitFormAdd.getForm().isValid())
					return;
				submitFormAdd.getForm().submit({
							url : '/stock/addOrEdit',
							method : 'POST',
							success : function(form, action) { 
								var isSuc = action.result;
								if (isSuc.error) {
									Ext.Msg.alert('提示', isSuc.rspMsg);
								} else {
									gridStore.load();
									o.ownerCt.ownerCt.close();
									if(submitFormAdd.items.items[0].getValue()&&submitFormAdd.items.items[0].getValue().length>0){
										Ext.Msg.alert('提示', isSuc.rspMsg);
									}else{
										var openWindow = Ext.widget('msgWindowView');
										openWindow.animateTarget = document.body;
										openWindow.title = "提示";
										ids=isSuc.id+"";
										openWindow.items.items[0].items.items[0].setValue(isSuc.rspMsg+",是否分配资源出库?");
										openWindow.show();
									}
									
								}
							},
							failure : function(form, action) {
								Ext.Msg.alert("提示", '出错！');
							}
						});
			},
			onLaunch : function() {
				grid = this.getViewportStock().items.items[3];
				gridStore=this.getStocksStore();
				ocbStore=this.getOperatorComboBoxStore();
				acbStore=this.getAccountComboBoxStore();
				shuntStore=this.getShuntComboBoxStore();
				modelStore=this.getModelComboBoxStore();
				acbStore.on('beforeload', function(store, options) { 
	      			Ext.apply(store.proxy.extraParams, {
	      			distributor_id : distributor_id
	      			
	      			});
				});
				ocbStore.on('beforeload', function(store, options) { 
						      			Ext.apply(store.proxy.extraParams, {
						      			 
						      			
						      			});
				});
				ocbStore.load();
				shuntStore.on('beforeload', function(store, options) { 
						      			Ext.apply(store.proxy.extraParams, {
						      			});
				});
				shuntStore.load();
				modelStore.on('beforeload', function(store, options) { 
						      			Ext.apply(store.proxy.extraParams, {
						      			});
				});
				modelStore.load();
				gridStore.on('beforeload', function(store, options) { 
	      			Ext.apply(store.proxy.extraParams, {
	      			stockState:Ext.getCmp('stock_state').getValue(),
	      			boxMac:Ext.getCmp('box_mac').getValue(),
	      			boxNumber:Ext.getCmp('box_number').getValue()
	      			
	      			});
				});
				gridStore.load();
			}
		});
